convert multiple single dimensional array to single multi dimensional array in php - php

I have 'N' single dimensional arrays(where N represents number of records retrieved by a sql command)
How do I convert it to multi dimensional array with keys as index?
At first I thought of creating a separate key array and then combining it with those N arrays
My output
Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and height are not proportional. )
Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo )
My expected output
Array ( [0] => Array ( [0] => PONDICHERRY [1] => 31-Jan-2018 [2] => [3] => distance and height are not proportional. ) [1] =>Array ( [0] => PONDICHERRY [1] => 03-Feb-2020 [2] => [3] => helloooooooooooooo ) )
It's difficult for me to combine the arrays, but how do i create like this?
Thank you

Rather than creating N separate arrays and then combining them later, you can combine them already while reading the rows.
$results = array();
while($row = $query->fetch_row()){
$results[] = $row;
}
// here you have $results in the expected output format

Related

Array push to one of my multidimentional array

I have array like below. I would like to push it to 0 element array.
$csvdata is contain original array $pushHeaderSpec variable is what i want to push into original array i have also tried array_merge but not work as expected merge well on output only but when i print original data in csv it is not there.i m generating $csvdata array first and then append this array on last.
Array
(
[Ruder] => no value need on this
[Glas] => no value need on this
[Not] => no value need on this
)
My Multidimention array look something like
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
)
)
Many more element on above array so i just want to merge my first array keys to this array on first element that is 0.
I did try using below code but it doesn't give me output what i want.
array_push($csvdata[0],array_keys($pushHeaderSpec));
Output from code
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] =>array (
[0] => Ruder
[1] => Glas
[2] => Not
)
)
)
Expecting Output
Array
(
[0] => Array
(
[0] => Sort order
[1] => Sku
[2] => Title
[3] => Ruder
[4] => Glas
[5] => Not
)
)
It was just
foreach (array_keys($pushHeaderSpec) as $key => $value) {
array_push($csvdata[0],$value);
}
Is that what you are looking for ?
foreach ($pushHeaderSpec as $key => $val) {
$csvdata[0][] = $key;
}

Check same word on each array

I want to check that a string contains the same word when comparing 2 different array's. If there is the same word on each array it will show how many on each inside multidimensional array
Array one is common array type and array two is multidimensional array
Array 1:
Array
(
[0] => royalty
[1] => free
[2] => picture
)
Array 2:
Array
(
[0] => Array
(
[0] => Affordable and search from millions of royalty free picture
)
[1] => Array
(
[0] => from millions of royalty picture
)
[2] => Array
(
[0] => Provides free picture upload and hosting
)
[3] => Array
(
[0] => Post your picture here Get permanent links
)
[4] => Array
(
[0] => Choose your own unique username to access image
)
)
Result:
Array
(
[0] => Array
(
[0] => 3
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 2
)
[3] => Array
(
[0] => 1
)
[4] => Array
(
[0] => 0
)
)
From the above example, array contain each word of royalty free picture will show how many word the same in each inside multidimensional array
I was trying it using strcasecmp() but it just give me 0 results if two string contains same word and I think it can't give right result for string with so many words.
Split string and find intersection with the array of seached words
foreach($array2 as &$item)
$item[0] = count(array_intersect($array1, explode(' ', $item[0])));
The easiest way is probably to loop through Array 2 and compare each word to the string with strstr() or preg_match
Something like this (incomplete):
foreach($array2 as $sentence){
foreach($array1 as $word){
if(strstr($word, $sentence) !== false){
$wordsFound++;
}
}
}

Need common arrays from two multidimensional arrays

Hi I have below multidimensional arrays -
Array
(
[user_attempts] => 0
[2] => Array
(
[0] => 1
[1] => 4
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
)
[1] => Array
(
[0] => Earth
)
)
and my second array is like below
Array
(
[1] => Array
(
[0] => Earth
)
[2] => Array
(
[0] => 2
[1] => 3
)
[3] => Array
(
[0] => 32
[1] => 23
)
[4] => Array
(
[0] => asdsa
[1] => asdas
)
)
I need to intersect both multidimensional arrays - so the result would be
Array
(
[1] => Array
(
[0] => Earth
)
[3] => Array
(
[0] => 32
[1] => 23
)
)
Can anyone help me to sort this out.
What I have tried is using array_intersect() but it limits to single array not multidimensional i guess.
PHP comes with a ton of functions already built in, but sometimes you still have to implement things yourself. What you want to do can be easily done by using the existing functions.
The goal is to do the following steps:
Find the keys that exist in both arrays
Loop through the array using these keys
Take the items of both input arrays with each of these keys
Calculate the intersection of those two arrays
Put it into a result array
Here is one way to do this:
function array_intersect_2dim (array $a1, array $a2) {
$keys = array_intersect(array_keys($a1), array_keys($a2));
$return = array();
foreach ($keys as $key) {
$return[$key] = array_intersect($a1[$key], $a2[$key]);
if (sizeof($return[$key]) == 0) {
unset($return[$key]);
}
}
return $return;
}
It works only for two dimensions. If you need more, you have to build a recursive approach, which follows the exact same principle.
To make the easier to compare you can use serialize/unserialize on this one. And then use array_intersect(). Try this example: Sample Output
$array1 = array( 'user_attemps' => 0, 2 => array(1, 4), 3 => array(32, 23), 4 => array('asdsa'), 1 => array('Earth'),);
$array2 = array( 1 => array('Earth'), 2 => array(2, 3), 3 => array(32, 23), 4 => array('asdsa', 'asdas'),);
$result = array_map('unserialize',array_intersect(array_map('serialize', $array1), array_map('serialize', $array2)));
print_r($result);

What does [1] => 0 mean in this array?

I know this must be a fairly simple question, but I haven't managed to stumble across an answer yet.
I have the following array
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
When I use print_r($qid) I get the following
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
I don't understand [1] => 0
in
[0] => Array ( [0] => 1 [1] => 0 )
If someone could explain what [1] => 0 means in this array, I'd greatly appreciate it. Thanks.
EDIT: It turns out that my array was indeed different to what I had written above, because it had been modified later in the code. Thanks everyone for the great answers. I'm still reading over them all and trying to make my mind understand them (Arrays turn my mind to jello).
[1] => 0 denotes an array element with the value 0.
The numbers in [] are array keys. So [1] is the second element of a numerically indexed array, (which starts with [0]), and the value of the second element ([1]) is 0.
PHP uses => as an operator to relate array keys/indices to their values.
So an overall explanation of this structure:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
The outer array is a numerically indexed array, and each of its elements is a sub-array. The first of them ([0]) is an array containing 2 elements, while the rest of them ([1] through [3]) are arrays containing only one single element.
That two-dimensional array is actually a one-dimensional array of arrays, which is why you're getting the nesting. The [x] => y bit simply means that index x of the array has the value y.
Now your output in this case doesn't actually match your code, since
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
produces:
Array (
[0] => Array ( [0] => 1 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
If you wanted to get:
Array (
[0] => Array ( [0] => 1 [1] => 0 )
[1] => Array ( [0] => 2 )
[2] => Array ( [0] => 3 )
[3] => Array ( [0] => 4 )
)
(with the first array having two elements), you'd actually need:
$qid[0][0]=1;
$qid[0][1]=0;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
print_r($qid);
You probably added a second item to $qid[0] somewhere ($qid[0][1] = 0). This code
$qid[0][0]=1;
$qid[1][0]=2;
$qid[2][0]=3;
$qid[3][0]=4;
outputs the the correct values for me (without [1] => 0:
Array ( [0] => Array ( [0] => 1 ) [1] => Array ( [0] => 2 ) [2] => Array ( [0] => 3 ) [3] => Array ( [0] => 4 ) )
It means that your index 0 in the original Array contains another Array of 2 items.
Specifically [1] => 0 means that the 2nd item of the "child" Array contains the number 0.
[1] => 0
in this simple way we can say that 1 is your array key and 0 is value for the 1 key
0 is store at the 1 key of the array
thanks
Simply put, you have a numerically indexed multidimensional array. http://php.net/manual/en/language.types.array.php should have all the information you need to read up on this.
As to why you have the [1] => 0, you'll need to look a little deeper into your code to see where it gets assigned.
I got the following result after printing out the array using print_r:
Array
(
[0] => Array
(
[0] => 1
)
[1] => Array
(
[0] => 2
)
[2] => Array
(
[0] => 3
)
[3] => Array
(
[0] => 4
)
)
I guess, you might have set a value for $gid[0][1] somewhere in your code.

Comparing Associative array and standard array PHP

I have two arrays
Array1:
Array ( [0] => Array ( [0] => 3 [1] => 1 [2] => 4 ) [1] => Array ( [0] => 1 [1] => 6 ) )
Array2:
Array ( [0] => 1 [1] => 3 [2] => 2 )
I used array_diff for comparing and getting the difference values, but the same key is coming ie.,
array_diff(Array1,Array2)
returns Array([0] =>3 [2] => 4)
but is there any other way to get difference and having result like
Array([0] =>3 [1] => 4)..
Assuming you've got array_diff working on the multidimensional array somehow, but from the docs:
This function only checks one dimension of a n-dimensional array. Of course you can check deeper dimensions by using array_diff($array1[0], $array2[0]);.
Use array_values around it.
array_values(array_diff($array1, $array2));

Categories