1

i have foreach loop that returns multi-array from the database and i want to convert this array to multi array in json ,

how to do this ?

php array example

Array
(
    [0] => Array
        (
            [it_code] => 2894
            [it_quantity] => 300
            [it_price] => 0
            [it_notes] => 
        )

    [1] => Array
        (
            [it_code] => 2894
            [it_quantity] => 284
            [it_price] => 0
            [it_notes] => 
        )

    [2] => Array
        (
            [it_code] => 2894
            [it_quantity] => 4
            [it_price] => 0
            [it_notes] => 
        )

    [3] => Array
        (
            [it_code] => 2894
            [it_quantity] => 3
            [it_price] => 0
            [it_notes] => 
        )

)

i want returned json to be like this format

 [
            ['2894', 300, 0,''],
            ['2894', 284, 0,''],
            ['2894', 4, 0,''],
            ['2894', 3, 0,''],
            ['2894', 10, 0, '']
        ]

my code like this

$this->db->where("it_parent_item", $parent_id);
$this->db->select("d_items.it_code,d_items_type.it_ty_ar_desc,d_items.it_quantity,d_items.it_price,it_notes");
$this->db->join('d_items_type','d_items_type.it_ty_id=d_items.it_type','left');
$this->db->from("d_items");
$result = $this->db->get()->result_array();


echo "<pre>";
print_r($result);
echo "</pre>";
5
  • is json_encode() what you're looking for? Commented Mar 2, 2016 at 15:51
  • @Henders, that won't remove the keys (if that's what the op is looking for), but it's worth a shot. So, do you have to remove the keys (it_..)? Commented Mar 2, 2016 at 15:53
  • Good point, didn't think about reformatting... Commented Mar 2, 2016 at 15:54
  • as you said i want to remove the keys before convert it to json Commented Mar 2, 2016 at 15:57
  • i want only the values Commented Mar 2, 2016 at 15:58

4 Answers 4

1

You can use array_values() and array_walk_recursive() to convert integer to string

$newArray = array();
foreach($sourceArray as $element) {
    $newArray[] = array_values($element);
}

array_walk_recursive($newArray, 
function(&$value, $key){
    $value = (string)$value;
});

print_r (json_encode($newArray));
Sign up to request clarification or add additional context in comments.

Comments

1

Note that other answers will give null instead of ''.

So, without using array_values, this code returns all values, but in case there is any null, it returns '' instead (as expected in the question):

$arr = array();
foreach($foo as $value){
    $tmp = array();
    foreach($value as $v){
        $tmp[] = $v===null ? '' : $v;
    }
    $arr[] = $tmp;
}

echo json_encode($arr);

Output:

[[2894,300,0,""],[2894,284,0,""],[2894,4,0,""],[2894,3,0,""]]

[
    [2894,300,0,""],
    [2894,284,0,""],
    [2894,4,0,""],
    [2894,3,0,""]
]

This a copyable array:

$foo = array
(
    0 => array
        (
            'it_code' => 2894,
            'it_quantity' => 300,
            'it_price' => 0,
            'it_notes' => null
        ),

    1 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 284,
            'it_price' => 0,
            'it_notes' => null
        ),

    2 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 4,
            'it_price' => 0,
            'it_notes' => null
        ),

    3 => Array
        (
            'it_code' => 2894,
            'it_quantity' => 3,
            'it_price' => 0,
            'it_notes' => null
        ),

);

2 Comments

Nice solution! Is this going to be more intensive than using array_values()?
TBH, I don't know if it will be more intensive, but in that case, array_values doesn't return '' when the value is null... @Henders
0

Here's the initial array (shown like a PHP array, but the same as your post):

$initialArray = array(
    array(
        "it_code"     => 2894,
        "it_quantity" => 300,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 284,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 4,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
    array(
        "it_code"     => 2894,
        "it_quantity" => 3,
        "it_price"    => 0,
        "it_notes"    => '',
    ),
);

You can loop over each element, assigning just the values to a new set of arrays, like this:

$newArray = array();

foreach ($initialArray as $subArray)
{
    $newArray[] = array_values($subArray);
}

The resulting array will look like this:

[[2894,300,0,""],[2894,284,0,""],[2894,4,0,""],[2894,3,0,""]]

3 Comments

syntax error, unexpected ','on: it_notes => ,
Fixed, those pesky null values for it_notes.
Sure, depends what the user wants. But hopefully these will get him started on reformatting the array, and maybe the SQL query can return more useful values instead of null.
0

Looks to me like you want to loop through your array so it's formatted how you want in PHP and then convert that PHP array into JSON:

$dataArray = array(); //The array containing your values
$jsonArray = array(); //The array which will be formatted for json

foreach($dataArray as $value){
    $keylessValues = array_values($value);
    $jsonArray[] = $keylessValues;
}

$jsonArray = json_encode($jsonArray); //This is now a JSON array storing your values

What we do here is move through the array and then take only the values with array_values() and put them into a new index in our $jsonArray.

Once we have moved through the entire array we can convert our newly formatted and populated array into JSON with json_encode()

It's worth noting that your values that are set as '' will come through as null. If you need those values as '' instead of null have a look at the answer @FirstOne gave.

2 Comments

Just note that instead of '', this returns null.
Thanks, I discovered that in my test but forgot to put it in!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.