Create multidimensional array from single array - php

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?

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

Access JSON Array Elemenets by Name Rather Than Index - PHP

Using the below JSON fragment as an example, if I wanted to access the Value of Year, I would use something like
$result->ItemSpecifics->NameValueList[1]->Value[0] .
However, this only works if you know the index of the Year array element (1 in this case).
My question is, if I don't know what the array index of Year is, how can I still access it' value? Is there a way to find the Year element by name rather than index?
This is not correct but I would expect a solution like:
$result->ItemSpecifics->NameValueList['Year']->Value[0]
JSON Example:
[ItemSpecifics] => stdClass Object
(
[NameValueList] => Array
(
[0] => stdClass Object
(
[Name] => Returns Accepted
[Value] => Array
(
[0] => ReturnsNotAccepted
)
)
[1] => stdClass Object
(
[Name] => Year
[Value] => Array
(
[0] => 2001
)
)
[2] => stdClass Object
(
[Name] => Manufacturer
[Value] => Array
(
[0] => Porsche
)
)

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);
}

PHP Printing JSON value

I have a JSON array that I want to be able to drill down to a lower level and print just that value. The problem occurs when I reach a level that has is indacted as [0] (or [n]). For example I have the following output, and I want to just print the game key for the first league.
This is how I am trying to print it
HtmlSpecialChars(print_r($user->fantasy_content->users[0]->user[1]->games[0]->game[0]->game_key,1))
However I keep getting this error:
Cannot use object of type stdClass as array
When I do it incrementally it seems to fail on this command (so I assume I'm not index correctly):
$user->fantasy_content->users[0]
Here is the output:
stdClass Object
(
[fantasy_content] => stdClass Object
(
[xml:lang] => en-US
[yahoo:uri] => /fantasy/v2/users;use_login=1/games
[users] => stdClass Object
(
[0] => stdClass Object
(
[user] => Array
(
[0] => stdClass Object
(
[guid] => IYEZUHTVBYRLIB3OAQC5WRZPQY
)
[1] => stdClass Object
(
[games] => stdClass Object
(
[0] => stdClass Object
(
[game] => Array
(
[0] => stdClass Object
(
[game_key] => 147
[game_id] => 147
[name] => Baseball
[code] => mlb
[type] => full
[url] => http://baseball.fantasysports.yahoo.com/b1
[season] => 2006
)
)
)
[count] => 1
)
)
)
)
[count] => 1
)
[time] => 52.390813827515ms
[copyright] => Data provided by Yahoo! and STATS, LLC
[refresh_rate] => 60
)
)
For objects you must use the -> syntax and if the key/property name is a number or has other special characters, you will need to use the $object->{'0'} syntax.
The game_key can be retrieved using:
$user->fantasy_content->users->{'0'}->user[1]->games->{'0'}->game[0]->game_key;
You can convert a stdClass object to an array by casting it like so:
<?php
$array = (array) $myObject;
echo json_encode($array);
You can also cast inline:
<?php
echo json_encode((array) $object);

Categories