Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #16809 > unrolled thread
| Started by | alex <alex@nomailno.it> |
|---|---|
| First post | 2016-07-01 10:46 +0200 |
| Last post | 2016-07-02 11:30 +0200 |
| Articles | 3 — 2 participants |
Back to article view | Back to comp.lang.php
how extract key and valu from object alex <alex@nomailno.it> - 2016-07-01 10:46 +0200
Re: how extract key and valu from object Ben Bacarisse <ben.usenet@bsb.me.uk> - 2016-07-01 15:12 +0100
Re: how extract key and valu from object alex <alex@nomailno.it> - 2016-07-02 11:30 +0200
| From | alex <alex@nomailno.it> |
|---|---|
| Date | 2016-07-01 10:46 +0200 |
| Subject | how extract key and valu from object |
| Message-ID | <nl5alr$g20$1@gioia.aioe.org> |
with print_r ($opzioni_select);
I have this:
Array (
[all] => Select
[0] => stdClass Object ([option] => Array ([29] => A))
[1] => stdClass Object ([option] => Array ([28] => B ))
[2] => stdClass Object ([option] => Array ([31] => C ))
)
after with
foreach ($opzioni_select as $key)
{
print_r ($key->option); echo '<br>';
}
heve this:
Array ([29] => A)
Array ([28] => B)
Array ([31] => C)
I would like to create a new array
$newArr[ array_keys($key->option) ] = array_values($key->option);
but have this error: The first argument should be an array
how can get for every element the key and the value?
An array where I can to extract for every elements the key and the value
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2016-07-01 15:12 +0100 |
| Message-ID | <87h9c98n2f.fsf@bsb.me.uk> |
| In reply to | #16809 |
alex <alex@nomailno.it> writes:
> with print_r ($opzioni_select);
var_dump gives you more detailed information.
> I have this:
> Array (
> [all] => Select
> [0] => stdClass Object ([option] => Array ([29] => A))
> [1] => stdClass Object ([option] => Array ([28] => B ))
> [2] => stdClass Object ([option] => Array ([31] => C ))
> )
>
>
> after with
> foreach ($opzioni_select as $key)
> {
> print_r ($key->option); echo '<br>';
>
> }
>
> heve this:
> Array ([29] => A)
> Array ([28] => B)
> Array ([31] => C)
And a notice that you are "Trying to get property of non-object". If
you don't see this you should probably turn notices on since they can
help a lot.
You need to avoid accessing $key->option when the $key is 'all'.
> I would like to create a new array
> $newArr[ array_keys($key->option) ] = array_values($key->option);
> but have this error: The first argument should be an array
>
> how can get for every element the key and the value?
It's possible that you want
$newarray[array_keys($obj->option)[0]] = array_values($obj->option)[0];
but, if not, please give the actual values you'd like to see in
$newarray. Also note that you will have to deal with the 'all' case as
I mentioned above.
> An array where I can to extract for every elements the key and the value
This bit I don't follow. You can do that for every array.
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | alex <alex@nomailno.it> |
|---|---|
| Date | 2016-07-02 11:30 +0200 |
| Message-ID | <nl81k9$cip$1@gioia.aioe.org> |
| In reply to | #16810 |
thanks for you suggestion I must see with calm
I find this solution also if isn't short
seen that
print_r ($op_select);
I have this structure
Array (
[all] => Select
[0] => stdClass Object ([option] => Array ([29] => A))
[1] => stdClass Object ([option] => Array ([28] => B ))
[2] => stdClass Object ([option] => Array ([31] => C ))
)
I solved so
$new = array();
$i=0;
foreach ($op_select as $key) {
$i++;
if ($i==1)
{
$new['all'] = $key ;
}
else
{foreach ( $key->option as $key1 => $val1)
{echo $key1.' '.$val1.'<br/>';
$newArr[ $key1 ] = $val1;}
}
}
only for first key
I set manually the key All and assign value Select by $key
this is a cicle also if inner variable I have only on element
after I take key and value by
Array ([29] => A)
Array ([28] => B
Array ([31] => C
with foreach ( $key->option as $key1 => $val1)
but think this isn't the better solution
but work;
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.php
csiph-web