2

I have an array like this:

Array
(
    [0] => stdClass Object
        (
            [㐀] => Array
                (
                    [0] => jau1
                )

        )

    [1] => stdClass Object
        (
            [㐁] => Array
                (
                    [0] => dou6
                )

        )

    [2] => stdClass Object
        (
            [㐂] => Array
                (
                    [0] => cat1
                )

        )

)

How can I remove the stdClassObject for every element in this array?

Since the key for each element is different, I guess array_column is not going to work.

1
  • How is this array generated, it may be easy to fix that. Commented May 15, 2020 at 6:27

2 Answers 2

4

you could just iterate the data and get what you want:

$res = array();
foreach ($array as $key => $val) {
    foreach ($val as $keyObj => $valObj) {
        $res[$keyObj] = $valObj[0];
    }
}

var_dump($res);

This outputs:

array(3) {
  ["㐀"]=>
  string(4) "jaul"
  ["㐁"]=>
  string(4) "dou6"
  ["㐂"]=>
  string(4) "cat1"
}

online demo

Sign up to request clarification or add additional context in comments.

Comments

1

suppose.. $array is your main array

you can try (if you want to convert object array element to array):

$arrCnt = count($array);   
for($i=0;$i<$arrCnt;$i++) $array[$i] = (array) $array[$i];

actually you have not mentioned your query exactly. Its confusing

Or

If you want to skip stdObject from that then you can try:

$arrCnt = count($array);
$newArr = array();
for($i=0;$i<$arrCnt;$i++){ 
   $array[$i] = (array) $array[$i];   
   foreach($array[$i] as $k=>$v) $newArr[$k] = $v[0];
}
print_r();

1 Comment

Altough OP doens't mention it, this answer does not preserve the keys like 㐀 㐁 㐂. And never use count in a for loop, as it is evaluated each itteration, adding a lot of unnecessary overhead

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.