How to retrieve an element from this PHP array? - php

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

Related

How to parse array in PHP.

I have the following array I want to get the element one by one.
"attachment":"["Apple","car","home","scooter"]"
If the array is in JSON form you do json_decode and the iterate over the array

Building array in loop with array push. Will using end() damage the array?

Maybe this is obvious, but end() returns the last array element and moves the pointer. It's that "moves the pointer" langauge that makes me nervous though. When using array_push or $thearray[] = "" or any other method of appending to the array, will the use of end() mean that the next added element will overwrite the last existing element?
Only the array functions like next, end, reset, each, etc all use the array internal pointer.
array_push() will always push elements to the end of the array and things like array_shift() will always shift elements off the start.
To learn more about the internal pointer, check out this other answer

Is it possible to get all the nodes with a specific name from a PHP array?

I am converting to a PHP array from a Facebook API response in JSON format. The result is a multilevel array with many elements named id on every level.
I would like to retrieve all the elements with the name id and I was wondering if there is any direct way to get all those elements without having to parse each level of the array to grab the element.
Hope this question makes sense,
Any tip will be much appreciated.
Use Array Column function
$output = array_column($input, 'id');
If you do not have php 5.5 (i.e. no array_column for you), you may use this:
$ids = array_map(function($element) {return $element['id'];}, $input);

PHP Specific Array Organization Function

I have a few session arrays which I happen to be removing specific indexes from. For example, I have a session named $_SESSION['products'], this session has these elements: $_SESSION['products'][0], $_SESSION['products'][1], and $_SESSION['products'][2].
I am trying to remove any one of those variables, the problem is when you remove the second variable, you mess up the array so that it cannot be displayed in a for loop. Is there a way to rearrange the following: $_SESSION['products'][0] and $_SESSION['products'][2] to $_SESSION['products'][0] and $_SESSION['products'][1] with a PHP built-in function? If not, is it even possible?
You can achieve this with array_values like:
unset($_SESSION['products'][2]); // assuming the product key
// exist in product array sess
$_SESSION['products'] = array_values($_SESSION['products']);
Manual
array_values() returns all the values from the array and **indexes the array numerically.**

interpreting JSON with PHP and putting it in array

I have only worked with JSON once before, but I don't recall how to use it with PHP.
If I have a script that returns JSON like this:
{
"bunisess":[
"business",
"bonuses",
"burnooses",
"boniness",
"burnoose's"
]
}
how can I take that and make each value a value in a PHP array. The keys just being numbers from 0 onwards?
Use json_decode, but pass true as the second parameter to get an associative array back:
$json='{"bunisess":["business","bonuses","burnooses","boniness","burnoose\'s"]}';
$data=json_decode($json, true);
Now, you can use array_values to get a numerically indexed array as you required:
$data=array_values($data);
A simple google search could have lead you to this page : http://php.net/manual/fr/function.json-decode.php
The subarray will be respecting exatcly what you're asking for.
Assuming that $data is the complete JSON string
$stdObject = json_decode($data, true);
print_r($array);
You should get an array with one key bunisess and value another numeric array with the other values.

Categories