Converting indexed array to normal or simple array - php

I am trying to convert indexed array to normal array. Basically what get is:
Array ( [0] => 14 [1] => 19 [2] => 20 )
And I need is:
Array(14,19,20);
I tried over Google but not information found. I think this kind of function isn't available in PHP, is there any? please let me know!
Thanks,
Asif

You're chasing shadows:
Both of the arrays you've shown are equal.
There is no such thing as an unindexed array in PHP.
But if you really want to be sure, use $newArray = array_values($array)

Related

Sorting multi-tier and multi-dimensional array in PHP

My first question post here, although I'm a long time user. My PHP skills aren't advanced. I'm using PHP7 BTW.
I've found similar questions to this one, but not with the tier element, and so none of the answers I've tried so far have worked. It's probably obvious to a more experienced PHP programmer.
I've got some data in the json format, and decoded it into the following array:
Array (
[soc] => 3421
[series] => Array (
[0] => Array ( [year] => 2013 [estpay] => 620 )
[1] => Array ( [year] => 2015 [estpay] => 580 )
)
)
The data source varies, and so some times there are more years data available and sometimes less. It will also change over time as new data is added.
I want to sort the array so that the most recent year is always first. As I mentioned earlier, I've tried a few of the solutions that have been posted on here for multidimensional arrays, but the data I'm using has the multidimensional array in a lower tier of the array, and so I haven't been able to work out how to change the example code for multidimensional arrays to work around this.
As I mentioned, I'm no PHP expert, so please make no assumptions on my skill level in any replies. Thanks for your help!
Assuming you're using PHP7, and as you said you want to order by year descending, then for each array that you want to sort, try the below:
usort($array, function ($a, $b) {
return $b['year'] <=> $a['year'];
});

Show array as written with rand inside

I have an array such as:
$var = array('hi','ho',rand(2,5));
What I would like to echo is the entire array, exactly as written.
Normally when you try a print_r, it shows as:
Array (
[0] => hi
[1] => ho
[2] => 3
)
But I want:
Array (
[0] => hi
[1] => ho
[2] => rand(2,5)
)
You can get this with file_get_contents, but is there any way to do so within the actual PHP file?
I don't think it's possible because when array is created, random value is assigned to element with index 2 and you cannot check how this value was created.
I don't think it's possible, since the rand is already evaluated as soon as you set the array to some variable.
A workaround would be the hold the expression as a string and then eval it when you need it. Like this:
$varStr = "array('hi','ho',rand(2,5))";
echo $varStr;
// when you actually need it
$var = eval($varStr);
However, this is almost never a good idea. Providing a use-case where you need this might help come up with a better solution.

Snytax for accessing multidimension array

I am trying to connect to a linkedin api and this is almost embarassing to ask but I just cant seem to get the syntax correct for accessing a multidimensional array. When I print out the full $information variable I get the following:
Array
(
[person] => Array
(
[id] => vlofsVJ8FM
[first-name] => Tyler
[last-name] => Slater
[interests] => Golf, Motorcycles, Technology
)
)
So when I go to try and reference the id I would normally say $information['person']['id'] but then it doesn't return anything. Sorry for the noob question but I just can't find the answer.
It looks like you have more arrays, so select the first
$information[0]['person']['id']
$information[0]['person']['element_name'];
Another way to get the data you need from this data structure can be done using the current function:
$information = current($information);
Then you can access the elements you need by using $information['id']

Parsing php array in python

I'm getting a PHP array from a web page (as a string).
It looks like :
Array
(
[k1] => Array
(
[a] => Array
(
[id] => 1
[age] => 60
)
[b] => Array
(
[id] => 2
[age] => 30
)
)
[k2] => v2
)
I want to parse it in python.
Does anyone have an idea how to do this?
Thanks,
Rivka
Edit:
This is really not a json, like a few people commented.
Thanks for the comments, and I updated the question.
That's not JSON, that's just how PHP prints arrays. If you want to create JSON of the array, check out json_encode for PHP. Then use Python's JSON library (or here for py3) to read it.
If I understood you correctly, you are using print_r on array to get that output. This is a visual representation of array only, you can't really parse it. For example:
array('Array'.PHP_EOL."\t(".PHP_EOL." [0] => test".PHP_EOL."\t)")
will look exactly like
array(array('test'));
You should use some real serializing function to do what you want(json,serialize etc.);

Turn X arrays into a single array

I want to change the format of this array to match another one. One was pulled from a database, and the other was made using space delimted user input and then explode();
So here are the arrays I want to change, this is from the database (mysql_fetch_array). It grabs the single field from all rows.
Array
(
[name] => daniel
)
Array
(
[name] => alex
)
Array
(
[name] => richard
)
And here is what I want it to look like. This was the output of the user submitted values, space delimted and using PHPs explode() function.
Array
(
[0] => daniel
[1] => alex
[2] => david
)
What I want to be able to do is have these in the same format so that I can compare the two. The end goal is to be able to compare the two arrays (user input and database results), and create a final array containing only values that the user has inputted that the database doesn't already contain.
So using the data above this would be my final array:
Array
(
[0] => david
)
I would really appreciate help with the first bit, and if anyone else has a better way to achieve the end goal that would be a great extra bonus! (I get the feeling it might be easier to do this with SQL queries but I'm really confusing myself with these arrays)
Thanks!
array_diff($user_array, $database_array);
You can construct the $database_array like this:
$database_array = array();
//assuming each db record has only one value
//while there are still results in the db, do:
$database_array[] = reset($fetched_record);
See array_diff.

Categories