Multidimensional array to 2 dimensional in php - php

Is it possible to easily go from multidimensional array to 2 dimensional without looping through it in php? like [key,[key2,value]] to [key,value] that is from
$tstAry = array("a"=>array(1,"alpha"),"b"=>array(2,"beta"));
to
$tstAry2 = array("a"=>"alpha","b"=>"beta");
i tried playing with array_keys and array_values but couldn't combine it correctly

$tstAry2 = array_combine(array_keys($tstAry), array_column($tstAry, 1));
This got me the desired result, is there an easier way or this is it? like [][1]

Related

how to make merge array to single array in php?

Sorry, i want to merge two array in to single array. how to merge two array ? thanks for your suggestion code.
$data = [{"nama":"mike"}]
$data1= [{"nama":"huna"}]
What you've shown in the question isn't valid PHP. I'm going to assume you actually have two strings like this:
$data = '[{"nama":"mike"}]';
$data1 = '[{"nama":"huna"}]';
If that's not the scenario then you need to update your question.
Anyway if you have two JSON strings you want to merge, then first decode them into PHP arrays, and then run array_merge (https://www.php.net/manual/en/function.array-merge.php). Then encode the result back into JSON if you need to.
echo json_encode(array_merge(json_decode($data), json_decode($data1)));
This outputs:
[
{"nama":"mike"},
{"nama":"huna"}
]
Demo: http://sandbox.onlinephpfunctions.com/code/2bbfe350418a280cb55193369833d4426a9aae7c

PHP Push into array multidimensional

I'm trying to push some data into a multidimensional array, but can't find the correct syntax anywhere.
array_push($price, $row["ShopID"], $row["URL"]);
This is my code so far, but this does not make the array multidimensional, it just inserts each item as an individual row in the array. How can I fix this, and make the array multidimensional?
Are you pushing an array? Then:
array_push($price, array($row["ShopID"], $row["URL"]));
or maybe:
array_push($price, $row);

Merge two arrays without an element in php

I have two associative arrays, and I need to merge it into one array, without an element from first array.
Now it looks like that:
$result = array_merge(getFirstArray(), getSecondArray());
What's the best way to do it?
and I need to merge it into one array, without an element from first array.
Then remove it. Before or after merge.
If you don't want the element in the first array and you know which one it is, then just remove it before the merge:
$array1 = getFirstArray();
unset($array1['element1']); // or $array1[0] or whatever the index is;
$result = array_merge($array1, getSecondArray());
$result = array_filter(getFirstArray(),
array_merge(getFirstArray(), getSecondArray());

Can't flatten multidimensional array with lots of duplicates

I'm trying to create a script that, based on an input a?? creates an array of all the combinations and permutations of all words containing an a and two other characters from the alphabet.
Values are such as a, ab, ba, dab, bga etc - as you may see the array contains (or should contain) a weird amount of values.
The problem is that the functions I use in the script outputs even more values with many duplicates.
And for some reason I can not create a flattened array without duplicates. I tried to use array_unique() but it doesn't work here. I tried to use explode() and implode() to flatten the result array, but no success. Even if I succeed to create a string from the values, when I try to transform this string into an array, the result is again the actual multi-dimensional array.
This drives me crazy, and as you see the code, I'm a beginner in PHP.
Any help to transform the actual multidimensional array to a flattened one without duplicates is highly appreciated. An example: actually the array contains 12168 sub-arrays, and only the string a occurs 1456 times. What I need is an array that doesn't have sub-arrays and contains each results only one time.
The PHP code is available at here
and the output is here:
Have you tried something like:
$inputString = 'a??';
$array = array();
if (strpos($inputString, 'a') !== false && !in_array($inputString, $array)) {
$array[] = $inputString;
}
echo '<pre>'; print_r($array); echo '</pre>';

Php match values between 2 arrays

Guys i have a very awkward situation here , i am not sure whether am i taking a right approach or not.. i am trying to match the values between 2 arrays and then running if else statement... here's goes what i am trying to do
$array1 = array('html','php','js','css');
$array2 = array('php','python','html','java');
what i want is to make a check where the values of these 2 arrays matches to each other.
like php and html is common in both and also where it doesn't match.
Thanks
You mean like an intersection?
It's your need:
$result = array_intersect($array1, $array2);
print_r($result);
the result is:
Array
(
[0] => html
[1] => php
)
array_intersect
and
array_diff
should do what you want.
To get both the intersecting elements of the array and the differing elements use both array_diff() and array_intersect().

Categories