PHP Push into array multidimensional - php

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

Related

How to retrieve an element from this PHP array?

So basically I have this array stored in SQL and I'm trying to retrieve the first element from it using PHP
I'm not sure if this is an associative or multidimensional array
a:3:{i:0;s:11:"Downpayment";i:1;s:28:"Variable 1 ";i:2;s:28:"Variable 2";}
How do I extract elements from this array ?
This is a PHP representation of serialized data. Use unserialize() on it.
If you not sure in this array you can use current() function or foreach to get first element of this array
For set inner pointer on first element use reset()

php function sometimes pushing arrays other times objects

I have an app which i'm making a HTTP request via PHP to retrieve some XML data. I'm then taking a string in that data, splitting it by spaces (" ") and pushing it into a new array. I'm then taking an existing array and de-duping it against this new array using the function array_diff. Here is my code below:
// THIS ARRAY IS MUCH LONGER BUT HAVE JUST PUT IN A FEW WORDS
$stopwords = array("a", "about", "above", "above", "across", "after", "afterwards", "again");
// this is my XML call
$test = simplexml_load_file('https://some-xml-endpoint.com/endpoint');
// defining a blank array where I want the result to go
$mappedWordsObj = [];
for($i = 0;$i < count($test->data);$i++) {
// if I echo this it returns me a string with no quotes (sometimes the quotes would be in there and I thought this may have been the issue)
$comment = chop(strtolower($test->data[$i]->comments));
// here I split the comment into an array by spaces
$wordsArray = explode(' ', $comment);
//here I compare my new array of words with the stopwords I want removed from the wordsArray
$arr_1 = array_diff($wordsArray, $stopwords);
// here I push into the $mappedWordsObj array
array_push($mappedWordsObj, $arr_1);
}
// here I push to the DOM the result to see how my array looks
echo json_encode($mappedWordsObj);
My issue is that in the resulting $mappedWordsObj array, I expect all the array items to be arrays themselves that contain the words but some of the items are getting inserted in the $mappedWordsObj as arrays with the words and others as objects with properties whos values are the words. Here is a snippet of the data returned:
{"0":"i","1":"just","2":"want","4":"switch","6":"existing","7":"t-mobile","8":"pay","13":"ee","14":"pay","15":"monthly.","16":"i","17":"don't","18":"want","20":"new","21":"phone!","23":"page","24":"does","26":"tell","31":"this!"},{"0":"just","2":"option","4":"'sim","5":"only'","9":"'radio","10":"button'","11":"forced","12":"selection."},
["voice","recignition"],
["testing","ol"],
{"0":"can't","1":"think","4":"i","7":"simple","9":"easy"},{"2":"instead","3":"lol"},
["n\/a"],
["great","website"],
I'd like to just have an array of arrays so can anyone please tell me where i've gone wrong?
Cheers
When you use json_encode, it will convert PHP arrays to either JSON arrays or JSON objects. Which one is output depends completely on the array keys in the PHP array. For PHP arrays with sequential, numeric indexes starting at 0, the JSON output will be an array. For PHP arrays with any other indexes, the JSON output will be an object.
The array_diff produces an array where the indexes are not sequential in some cases. You can use array_values to reindex the result before appending it to your output array.
array_push($mappedWordsObj, array_values($arr_1));
Side note - array_push actually isn't necessary here. You can use
$mappedWordsObj[] = array_values($arr_1);
The array_push documentation actually recommends doing it this way when you're only appending one item to the array. But, it's a pretty small optimization, so if array_push looks better to you, never mind. :)

Multidimensional array to 2 dimensional in 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]

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>';

Grabbing specific values from a multi-dimensional array in PHP

I'm new to programming and I'm tackling arrays. I thought I had multi-dimensional arrays figured out but I guess I don't. Here's the code I'm working on:
$loopcounter = 0;
while ($myrow = mysql_fetch_array($results)) {
//...other stuff happens...
$allminmax[$loopcounter][] = array("$myrow[3]","$currentcoltype","$tempmin","$tempmax");
$loopcounter++;
}
This chunk of code is supposed to create an array of four values ($myrow[3], currentcoltype, tempmin, tempmax) and insert it into another array every time the loop goes through. When I do this:
echo implode($allminmax);
I get:
ArrayArrayArrayArrayArrayArrayArrayArrayArray
Do I need to implode each array before it goes into the master array? I really want to be able to do something like $allminmax[0][4] and get the $tempmax for the first row. When I try this now nothing happens. Thanks -- any help is appreciated!
It looks like you should either use [$loopcounter] or [], but not both. You also should drop the quotes. They're unnecessary and in the case of "$myrow[3]" they interfere with the variable interpolation.
$allminmax[] = array($myrow[3], $currentcoltype, $tempmin, $tempmax);
By the way, arrays are zero-indexed, so to get $tempmax for the first row it'd be $allminmax[0][3] not $allminmax[0][4].
Also, a better way to display the contents of your array is with print_r or var_dump. These will display arrays within arrays while a simple echo will not.
var_dump($allminmax);

Categories