Using PHP I am running a query on my database and as a result I get a data-set with optional values. An example:
Result:
Array
(
[0] => Array
(
[attribute_1] => Red,
[attribute_2] => Car,
[name] => Sportscar
)
[1] => Array
(
[attribute_1] => Red,
[attribute_2] => ,
[name] => Rose
)
)
I want to format the resulting Data as such:
Array
(
[Red] => Array
(
[Car] => Array
(
[0] => Sportscar
)
)
[0] => Rose
)
So that I could access the Sportscar by
$array['Red']['Car'][0];
And the Rose by
$array['Red'][0]
This will make it easier to output the data with a nice little foreach loop.
of course the actual case is a bit more complex with a lot more values etc but this example demonstrates the principle.
The problem is that I can't think of a proper way to format the data efficiently. Not necessarily recursive but at least not one million if and elses would be nice.
Any ideas?
Related
I have been trying to use some logic to be able to see if all elements in a multidimensional array (haystack) exits in another much larger haystack. My first haystack comes from a json object in the first place, and the second one is a "dot name" string, that is also converted into an array. The logic here is that I can use a string describing what I'm looking for in the json in a simple way:
Needle:
Array
(
[key1] => Array
(
[provider] => Array
(
[id] => 1
)
)
[key2] => Array
(
[consumer] => Array
(
[id] => 5
)
[hostname] => foo
)
)
Haystack:
[0] => Array
(
[id] => 1000
[consumer] => Array
(
[id] => 5
[name] => test
[hostname] => foo
[1] => Array
(
[id] => 1200
[provider] => Array
(
[id] => 5
[name] => test
If the needle exists in the array the top key should be returned. So if we pretend all key->values below "key2" exists in array we should return key2. provider-id and consumer-id are not the same and there might be other key-values that might look the same, but has a different path. so if "hostname" would exists, but much deeper into array that is not a match.
The problem is that all examples I've come across either have:
A fixed depth on the array.
Having the needle as a string.
Is not true multidimensional.
Is there a way this can be done? recursiveiteratoriterator did come to my mind but not sure actually how to use it, based on the above.
I've also looked at various in_array/is_array/array_map/array_filter recursive examples but they are fell a bit messy and I couldn't decide if it was worth trying to build something that would allow to crawl two arrays in a good way.
My array is quite large and contains, sadly mostly items I'm not interested in. But if I can find a way that works I'm sure I can optimize that part.
Thanks for reading.
STARTING ARRAY
Array
(
[0] => Array
(
[0] => /searchnew.aspx?Make=Toyota&Model=Tundra&Trim=CrewMax+5.7L+V8+6-Spd+AT+SR5&st=Price+asc
[1] => 19
)
)
I have been struggling to break down this array for the past couple days now. I have found a few useful functions to extract the strings I need when a start and end point are defined, however, I can't see that being good for long term use. Basically I'm trying to take the string relative to [0], and extract the strings following "Model=" and "Trim=", in hopes to have array like this:
Array
(
[0] => Array
(
[0] => Tundra ***model***
[1] => CrewMax+5.7L+V8+6-Spd+AT+SR5 ***trim***
[2] => 19
)
)
I'm getting this information fed through an api, so coming up with a dynamic solution is my biggest challenge. I realize this a big question, but is there a better/less hacky way of approaching this problem?
parse_url() will get you the query string and parse_str() parses the variables from that:
$q = parse_url($array[0][0], PHP_URL_QUERY);
parse_str($q, $result);
print_r($result);
Yields:
Array
(
[Make] => Toyota
[Model] => Tundra
[Trim] => CrewMax 5.7L V8 6-Spd AT SR5
[st] => Price asc
)
Now just echo $result['Model'] etc...
The output for print_r($array) is as follow;
Array (
[0] => xmlrpcval Object (
[me] => Array ( [int] => 50 )
[mytype] => 1
[_php_class] =>
)
[1] => xmlrpcval Object (
[me] => Array ( [string] => Angel Cook (Chamber Works) )
[mytype] => 1 [_php_class] => )
)
I only want to print 'Angel Cook (Chamber Works)', Any help appreciated?
Hardcoded this would be something like the following:
print $array[1]->me['string']
Judged from the following:
You have an array... which contains entries... which are objects... which contain properties... which are associative arrays... which contain your values
Edit: not certain whether [string] is actually a key. If not... use me[0] instead.
You have an array of objects which you want the second object (array's are zero based), where the me key point to an array where you want the first item so:
$array[1]->me[0]
I am wondering what is better to do. I have a pulled back a query like this:
Array
(
[_id] => MongoId Object
(
[$id] => 4eeedd9545c717620a000007
)
[field1] => ...
[field2] => ...
[field3] => ...
[field4] => ...
[field5] => ...
[field6] => ...
[votes] => Array
(
[whoVoted] => Array
(
[0] => 4f98930cb1445d0a7d000001
[1] => 4f98959cb1445d0a7d000002
[1] => 4f88730cb1445d0a7d000003
)
)
)
Which would be faster:
Pull that entire array in 1 query and use in_array() to find the right id?
Pull everything from the first query except the votes and then do another mongodb query to see if that id exist in the array?
It Depends on a lot of factors that I suggest you test but IMO most of the time it would be faster to just do 2 querys
Depends on the size of the array being returned / searched.
Also different servers are doing the work, what do you mean by faster? At what scale?
I'm not even entirely sure how to ask the question, but assuming I have an array in memory like this:
Array (
[0] => Array
(
[0] => Array
(
[0] => 18451
[1] => MDX
)
[1] => Array
(
[0] => 18450
[1] => NSC
)
[2] => Array
(
[0] => 18446
[1] => RL
)
)
)
Is there some existing functionality to turn it into the code version of that array? I have a # of arrays I need to do this for, nested to various degrees. So I imagine I want output something like
$arrayname[] = array(array('18451','MDX'),array('18450','NSC'),array('18446','RL'));
I can write something to do it, but I'd rather not recreate the wheel if there's an existing way to do it.
This may be all you need:
http://us.php.net/var_export
manually loop through the array recursively and create a string like how you want.
var_export could help you there i think. The other option would be looping through it manually :(