Nested array, get items with same key - php

I have a small tricky question with nested arrays. I am getting something like that from my database:
array
0 =>
array
'id' => string '81' (length=2)
'value' => string 'foobar' (length=6)
'created_at' => string '2012-02-18 22:09:57' (length=19)
'updated_at' => string '2012-02-18 22:09:57' (length=19)
1 =>
array
'id' => string '106' (length=3)
'value' => string 'barfoo' (length=6)
'created_at' => string '2012-02-19 15:11:47' (length=19)
'updated_at' => string '2012-02-19 15:11:48' (length=19)
What I want to achieve now is to extract a simple associative array, where one "column" becomes the key and one "column" becomes the value. For the case id / value, the result should then look like that:
array
81 => 'foobar'
106 => 'barfoo'
I know that I could do nested loops to foreach through all of the arrays, but I was wondering if there is a quicker and more native method. I was playing around with array_intersect, but it does not seem to deliver what I need.

Well, this one doesn't involve nested loops:
$result = array();
foreach($queryResult as $row) {
$result[$row['id']] = $row['value'];
}

Related

How can i select array elements what name contains numbers? [duplicate]

This question already has answers here:
How to filter an array by a condition
(9 answers)
Closed 2 years ago.
At first, im beginner.
I want to push elements from array to another array what does not contain numbers
I have an array1:
0 => string '142221A' (length=7)
1 => string 'hOUSES' (length=6)
2 => string 'bOOKS' (length=5)
3 => string 'sHOES' (length=5)
4 => string '92921' (length=5)
5 => string '12231' (length=5)
6 => string 'cARS' (length=4)
7 => string 'tOYS' (length=4)
The output i want like this, array2:
0 => string 'hOUSES' (length=6)
1 => string 'bOOKS' (length=5)
2 => string 'sHOES' (length=5)
3 => string 'cARS' (length=4)
4 => string 'tOYS' (length=4)
I dont want a solution, i want the way for it.
in PHP you can use is_numeric() method to check the string is a just a numeric or not as the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$string_array = [];
foreach ($elements as $element) {
if(!is_numeric($element)) {
array_push($string_array, $element);
}
}
print_r($string_array);
but if you want to filter elements of an array to just have the elements that don't have any numeric value inside of it use the following way:
$elements = ['142221A','hOUSES','bOOKS','sHOES','92921','12231','cARS','tOYS'];
$just_string = [];
foreach ($elements as $element) {
//it will check for the element which has a digit number inside of it or not
//if it doesn't contain any number then it will be added to new array
if(preg_match('~[0-9]~', $element) != 1){
array_push($just_string, $element);
}
}
print_r($just_string);

I cant add any key value to my nested arrays

I have $monTab, an array with nested arrays like this in php :
array (size=12)
0 =>
array (size=2)
'mon' => string '2018-01-01 00:00:00' (length=19)
'nb_argus' => string '29' (length=2)
1 =>
array (size=2)
'mon' => string '2018-02-01 00:00:00' (length=19)
'nb_argus' => string '21' (length=2)
2 =>
I am simply trying to add this new pair of key value to each of the nested arrays :
'tx' => int '50' (length=2)
So i've built a for each like that :
foreach($monTab as $item) {
$item["tx"] = 50;
}
It doesnt work at all, var_dump($monTab) shows that nothing has happened !
the tx key is not added at all, the value is not added at all to my arrays !!
Due to the side effect of using pass by reference with foreach(...), using array_walk() or array_map() may be an idea.
array_walk($monTab, function(&$m){
$m['tx'] = 50;
});

Comparing two arrays using both keys and values

Hello i have two arrays one coming from the client and the other coming from my database, i want to be able to compare these two arrays and make sure they are both equal.
By equal i mean they both have the same keys and the keys have the same values:
array (size=2)
0 =>
array (size=5)
'id' => int 13
'class' => string 'Regular' (length=7)
'price' => int 100
1 =>
array (size=5)
'id' => int 13
'class' => string 'Regular' (length=7)
'price' => int 200
array (size=2)
0 =>
array (size=5)
'id' => int 13
'class' => string 'Regular' (length=7)
'price' => int 100
1 =>
array (size=5)
'id' => int 13
'class' => string 'Regular' (length=7)
'price' => int 300
In the above scenario y function shuld return false because even though my arrays have the same number of elements the price property of the second index is different, first array has 200 ad second array has 300.
Also if for some reason array 1 has more elements than array 2 then it should also return false.
What would be the best way of doing this? Bets in terms of speed and performance.
I was thinking of converting both arrays to json and checking them like a string.
Try this
$arraysAreEqual = ($a == $b); // TRUE if $a and $b have the same key/value pairs.
$arraysAreEqual = ($a === $b); // TRUE if $a and $b have the same key/value pairs in the same order and of the same types.
array_diff() is there for this purpose. And its ok if the arrays are small, but for optimization, check out this post. It involves flipping the array value and key for faster comparison. And also this other stack comment for a hash table approach.

PHP find and return neighbor value in multidimensional array

I have a multidimensional array like so:
array (size=4)
0 =>
array (size=2)
'term' => string 'news-article' (length=12)
'count' => int 139
1 =>
array (size=2)
'term' => string 'industry-resource' (length=17)
'count' => int 37
2 =>
array (size=2)
'term' => string 'editorial' (length=9)
'count' => int 33
3 =>
array (size=2)
'term' => string 'bulletin' (length=8)
'count' => int 12
and I'm trying create a function that searches for a term and returns it's neighboring value, count.
My inclination was to use array_search(), however using this returns false, I'm guessin because it's only searching the first layer of the array (0,1,2,3).
I'm not so much looking for an exact answer but a nudge in the right direction. I'm guessing it will require looping through the array, but I do not know how to approach getting the neighboring count value once the term value is located. Any help is appreciated!
You can just loop through the array and access them directly.
$search_term = "news-article";
$count = 0;
foreach($array as $element) {
if($element['term'] == $search_term) {
$count = $element['count'];
break;
}
}

Doctrine - query returns nested arrays instead of one

I'm doing this query
public function getRecommendedVendors($user)
{
$q = $this->em->createQuery(
"
select cat.id
from Zgh\FEBundle\Entity\Category cat
inner join cat.users u
where u = :user
"
);
$q->setParameters(["user" => $user]);
var_dump($q->execute());
die;
return $q->execute();
}
Doing var_dump() returns:
array (size=2)
0 =>
array (size=1)
'id' => string '1' (length=1)
1 =>
array (size=1)
'id' => string '10' (length=2)
Where I want it to return because I use the result inside IN statement:
array (size=2)
0 =>
'id' => string '1' (length=1)
1 =>
'id' => string '10' (length=2)
What you experience is the default query behaviour: The first array “level” are the rows, the second “level” are the columns. Of course, you only have one column, therefore it looks a bit strange.
Instead of execute() you should use getScalarResult(). To learn more about this, read http://docs.doctrine-project.org/en/2.1/reference/dql-doctrine-query-language.html#query-result-formats

Categories