PHP variable to array - php

i need to convert a PHP $var to a array.
i did like this
$array = array();
$var=$array;
but this way the dosent give the output as this
Array ( [0] => [1] => 9 [2] => 7 [3] => 11 [4] => 5 )
its gives like
a normal varible print 154515100

It depends on what the variable is. If it is a string, do:
$array = str_split($var);

What is the variable to begin with?
If it was an object, you could cast it to an array like
$var=(array)$var;
It sounds like you want to convert from a string or an integer to an array, though. Please provide more details.

You should define $var as array first

Related

Could not get data from serialized array using unserialize function

I have serialized data and I am using PHP unserialize function which is not working for me. I do not know what is wrong with this string. This is how my serialized data look like. My PHP knowledge is limited so I could not figure out the issue in this data. Can any one help me with this.
s:73:"a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}";
You have a serialized string that contains a serialized array. The string length is 81 not 73.
s:81:"characters in between the first and last quotes and in the example there are 81"
$string = 's:81:"a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}";';
$result = unserialize($string);
Yields the serialized array:
a:5:{i:0;s:4:"8941";i:1;s:4:"8939";i:2;s:4:"8942";i:3;s:4:"8946";i:4;s:4:"8950";}
Unserialize that:
$array = unserialize($result);
Yields the array:
Array
(
[0] => 8941
[1] => 8939
[2] => 8942
[3] => 8946
[4] => 8950
)

Get array value and put it into a variable (multidimensional array?)

When a print_r() an array $stats, I get the following:
Array ( [0] => Array ( [like] => 71 [dislike] => 372 [total] => 443 [like_s] => 78 [dislike_s] => 291 [total_s] => 369 [final] => 11 ))
I want to get the [dislike_s] value and put it into a variable.
I have attempted this:
$statss = $stats['dislike_s'];
But it did not work. I have also tried $statss = $stats['dislike_s'][0]; without result.
What am I doing wrong?
You are missing a level of your array
Array ( [0] => Array ( [like] => 71
//^ this level
Also you are using the wrong variable, as you said your array is stored in $stats so
$total_revision = $stats[0]['dislike_s'];
$stats is a two dimensional Array, i.e. it is an array of arrays. You can see this from the output of print_r. You have something that looks like `Array ( [0]=>Array(...)). Therefore, when accessing an element of the inside array, you can think of it like this:
$inner_array=$stats[0];
$total_revisions=$inner_array['dislike_s'];
Php gives you a shorthand for combining these steps(pretty much all languages do) and it looks like $total_revisions=$stats[0]['dislike_s'] but intuitively it's the same thing. You're saying "in the array $stats[0] give back the value of array element 'dislike_s'"

PHP, file_get_contents, for loop, str_replace

I am quite new with PHP and I am trying to read something form an API.
At the moment I use
$homepage = file_get_contents('http://www.site.com');
echo $homepage
This returns something which looks like this:
Array
(
[0] => Array
(
[name] => myname
[user_id] => 31232
)
[1] => Array
(
[name] => anothername
[user_id] => 23534
)
)
So here is what I want: I only want to read the [name] => x and leave the rest, so I tried a for loop within str_replace, but all I got were errors.
I hope someone is able to help me
Edit:
I just saw I could set is as a json text too, it returns something like
[{"name":"myname","user_id":"31232"},{"name":"anothername","user_id":"23534"}]
Edit2: Thank you Tuga, that was exactly what I was searching for :) I can't upvote, since my reputaion is below 15, is there another way for me to show your answer helped?
$array = json_decode($homepage); will return an array, then you can loop the array containing objects and use 'name' attribute :
foreach ($array as $obj) echo $obj->name;

How to change variable names inside array php

Ok so I have been struggling with this for hours now and I cannot seem to figure out what I'm trying to do. I have an array with many percent values placed inside and I'm printing out the first 5 of them. The $percent variables are acquired through similar_text
$array=array($percent12, $percent13, $percent14,
$percent15, $percent16, $percent17,
$percent18, $percent19, $percent110,
$percent111, $percent112, $percent113,
$percent114, $percent115, $percent116,
$percent117, $percent118, $percent119,
$percent120);
print_r(array_slice($array,0,5));
and it outputs like this:
Array ( [0] => 36.015505697169 [1] => 2.4181626187962 [2] => 2.4181626187962 [3] => 5.2153134902083 [4] => 100 )
So what i'm trying to figure out here is if it's possible to print the results of my array as they are listed above. example output would look like this:
Array ( [percent12] => 36.015505697169 [percent13] => 2.4181626187962 [percent14] => 2.4181626187962 [percent15] => 5.2153134902083 [percent16] => 100 )
i feel like this isn't possible, but if not, is there a way to assign the
[0]=> 36.015505697169 [1]=> 2.4181626187962
...etc to output something else say like:
[web0]=> 36.015505697169 [web1] => 2.4181626187962
Please help!! It's driving me crazy!!
You need to make it an associative array:
$array=array('percent12' => $percent12,
'percent13' => $percent13,
...);
I recommend using array_combine()
Basically you're just going to setup your new array with the keys, and pass in your current array for the values, thus creating a new array with the keys you want in the right place.
Try like
$myArr = array_slice($array,0,5);
$i = 0;
foreach($myArr as $key => $value) {
$newArr['web'.$i++] = $value;
}
print_r($newArr);

How do I `json_encode()` keys from PHP array?

I have an array which prints like this
Array ( [0] => 1691864 [1] => 7944458 [2] => 9274078 [3] => 1062072 [4] => 8625335 [5] => 8255371 [6] => 5476104 [7] => 6145446 [8] => 7525604 [9] => 5947143 )
If I json_encode($thearray) I get something like this
[1691864,7944458,9274078,1062072,8625335,8255371,5476104,6145446,7525604,5947143]
Why the name is not encoded (e.g 0, 1 , 2 , 3 etc) ? and how should I do to make it appear in the json code?
the full code is below
$ie = 0;
while($ie 10)
{
$genid = rand(1000000,9999999);
$temp[$ie] = $genid ;
$ie++;
}
print_r($temp);
$temp_json = json_encode($temp);
print_r($temp_json);
You can force that json_encode uses an object although you’re passing an array with numeric keys by setting the JSON_FORCE_OBJECT option:
json_encode($thearray, JSON_FORCE_OBJECT)
Then the returned value will be a JSON object with numeric keys:
{"0":1691864,"1":7944458,"2":9274078,"3":1062072,"4":8625335,"5":8255371,"6":5476104,"7":6145446,"8":7525604,"9":5947143}
But you should only do this if an object is really required.
Use this instead:
json_encode((object)$temp)
This converts the array into object, which when JSON-encoded, will display the keys.
If you are storing a sequence of data, not a mapping from number to another number, you really should use array.
Because those are just the indices of the array. If you want to add some kind of name to each element then you need to use an associative array.
When you decode that JSON array though it will come back out to 0, 1, 2, 3 etc.
This is defined behaviour. The array you show is a non-associative, normally indexed array. Its indexes are implicitly numeric.
If you decode the array in PHP or JavaScript, you will be able to access the elements using the index:
$temp_array = json_decode($temp_json);
echo $temp_array[2]; // 9274078

Categories