reindexing array inside object after unsetting few keys in php [duplicate] - php

This question already has an answer here:
Remove index based json object name in php [duplicate]
(1 answer)
Closed 1 year ago.
So here we have an array function array_values but it is working if performing on the same array. Over here i have an object and inside that objst i have set of arrays and with few condition i have to unset few of the keys and reindex the array.
stdClass Object
(
[details] => stdClass Object
(
[first] => 159
[events] => Array
(
[0] => stdClass Object
(
[id]=>1,
[name]=>abc
)
[1] => stdClass Object
(
[id]=>2,
[name]=>abc
)
[2] => stdClass Object
(
[id]=>3,
[name]=>abc
)
)
)
)
Now if i unset the 1 key and do json_encode and while printing it by doing json_decode, then it becomes
stdClass Object
(
[details] => stdClass Object
(
[first] => 159
[events] => stdClass Object
(
[0] => stdClass Object
(
[id]=>1,
[name]=>abc
)
[2] => stdClass Object
(
[id]=>3,
[name]=>abc
)
)
)
)
The events node becomes object, I don't want to change it from array to object. My result has to be exactly the same with reindexing the key.

Use array_values() to reindex an array.
$object->events = array_values($object->events);

Related

filter unique array std object data from 2 php arrays [duplicate]

This question already has answers here:
Compare 2-dimensional data sets based on a specified second level value
(9 answers)
Closed 12 months ago.
I have 2 array of std objects where i need to filter out array -> stdClass Object -> matching [code] and get final array with unique array -> stdClass Object -> [code]
here are examples array #1
Array
(
[0] => stdClass Object
(
[code] => 100
[c_price] => 438
)
[1] => stdClass Object
(
[code] => 1100
[c_price] => 105
)
)
here are examples array #2
Array
(
[0] => stdClass Object
(
[code] => 100
[c_price] => 1250
)
[1] => stdClass Object
(
[code] => 1100
[c_price] => 300
)
[2] => stdClass Object
(
[code] => 4807
[c_price] => 1000
)
)
Expected results i want to get
Array
(
[0] => stdClass Object
(
[code] => 4807
[c_price] => 1000
)
)
i have tried many answers but not found any closer to my problem, i have tried array_unique but it's not working because of std object class, thanks in advance
solved with
$arrdiff = array_diff_assoc($array2, $array1);
cheers # Kiran Rai Chamling

How I can access to these values in body question,

How I can fetch the values of "callback_data" of theses arrays for telegram bot when I click the inline button query
stdClass Object
(
[inline_keyboard] => Array
(
[0] => Array
(
[0] => stdClass Object
(
[text] => btn1
[callback_data] => v
)
[1] => stdClass Object
(
[text] => btn2
[callback_data] => r
)
)
)
)
You've got stdClass objects in your array, properties of which are accessed with arrow notation (->).
$yourVariable->inline_keyboard[0][0]->callback_data
$yourVariable->inline_keyboard[0][1]->callback_data

how get value feild[ID] stdClass Object in php code

how get value feild[id] in php code
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
You didn't give us a name of stdClass so I'm assuming it's $stdClass.
$stdClass->List_inserted[0]->ID
Let's break it down;
stdClass Object ( [List_inserted] => Array ( [0] => stdClass Object ( [ID] => 145001 [value] => 40 ) ) [Sucssess] => 1 [ErrorMassage] => OK )
We access objects with a -> and we access arrays with []
The first part tells us it's an object, so it's;
$stdClass->List_inserted
List_inserted is an array thanks to the => Array. We can access this with [0].
$stdClass->List_inserted[0]
Well, List_inserted[0] is an object, thanks too [0] => stdClass Object; and you wanted to access the ID? So we need another ->
$stdClass->List_inserted[0]->ID

PHP - Changing an Object for Consistency

I have a scenario where an API is returning multiple records inside object containing a numeric array like so;
stdClass Object
(
[Event] => Array
(
[0] => stdClass Object
(
[ID] => 111
[Name] => My First Event
[EventType] => stdClass Object
(
[ID] => 1
[Category] => Music
)
)
[1] => stdClass Object
(
[ID] => 222
[Name] => My Second Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 2
)
I'm current using a foreach loop to iterate through the records. This works fine.
foreach($result->Event as $Event)
But there is a problem here I have a scenario where a single results is returned in the object like so;
stdClass Object
(
[Event] => stdClass Object
(
[ID] => 11
[Name] => My Only Event
[EventType] => stdClass Object
(
[ID] => 2
[Category] => Sport
)
)
[Errors] => stdClass Object
(
[Result] => 0
[Message] =>
)
[RecordCount] => 1
)
Notice there is no [0] array index for the single results.
What's the best way to overcome this keeping in mind that I have no control of the data returned by the API?
Check if Event is an array or an object
if( is_object( $result->Event ) )
{
// ...
}
else
{
foreach( // [....]
}
You may process the object or overwrite it with a 1 item array as suggested by Sam
Btw: very bad API design. I would complain....
The best workaround I have found is to add the single Event to an array with a zero index within the result object. This way the result object matches the same structure as a result containing multiple records.
if(!is_array($result->Event)){
$result->Event = array($result->Event);
}

Create multidimensional array from single array

I have the following sql resultset after querying a mysql table.So it's producing an array having each row as single element.
Array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
[3] => stdClass Object
(
[quid] => 9
)
[4] => stdClass Object
(
[quid] => 10
)
[5] => stdClass Object
(
[quid] => 11
)
)
Now I would like to create another array using php where the first key will hold all the entire array resultset and so on.
Array
(
[0] => array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
)
[1] => array
(
[0] => stdClass Object
(
[quid] => 9
)
[1] => stdClass Object
(
[quid] => 10
)
[2] => stdClass Object
(
[quid] => 11
)
)
)
I'll happily edit/update this answer as further details are provided.
If I may, I think your issue is further "upstream" than your PHP code. I'd suggest something in your database schema is missing...
As best I can tell - (your question needs more detail to be honest) - you've got two dimensional information in your database:
[x,y]
So we could see this as:
[0, 9],[0,10],[0,11]
[1, 9],[1,10],[1,11]
If in your schema you have two columns - x,y - you only need select both and parse appropriately in PHP:
<?php
$arr = array();
$res = do_query("SELECT x,y FROM tbl;");
while ($row = get_row($res))
$arr[$row['x']] = $row['y'];
etc.
If you don't have both columns in your database, I'm completely unclear as to how you're going to procedurally break 1-dimensional information into 2.
Perhaps you could demonstrate your schema and some sample data, and provide a fuller explanation of the problem you're trying to solve?

Categories