Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.php > #16810

Re: how extract key and valu from object

Path csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail
From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.php
Subject Re: how extract key and valu from object
Date Fri, 01 Jul 2016 15:12:08 +0100
Organization A noiseless patient Spider
Lines 53
Message-ID <87h9c98n2f.fsf@bsb.me.uk> (permalink)
References <nl5alr$g20$1@gioia.aioe.org>
Mime-Version 1.0
Content-Type text/plain
Injection-Info mx02.eternal-september.org; posting-host="017616aa25f81ec581c44d76d61ba2f3"; logging-data="1652"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/h3mNbIaWcquH/hQh1qWAmwDYxJ5QgMV4="
Cancel-Lock sha1:/zvqhJrEW4eKbvhZO/mx4mUPUSE= sha1:Ijn7DXxy/jPizZMMsCmARU+pWE0=
X-BSB-Auth 1.16ea8ea11e1c68e8303b.20160701151208BST.87h9c98n2f.fsf@bsb.me.uk
Xref csiph.com comp.lang.php:16810

Show key headers only | View raw


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.

Back to comp.lang.php | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

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

csiph-web