为另一个数组创建数组值键

时间:2017-06-12 12:05:36

标签: php mysql arrays

我在这样的表中有价值:

#!/bin/bash

# read complete file in an array
mapfile -t arr < file

# do some calculations
n=$( echo "${arr[0]} + ${arr[1]} - ${arr[2]}" | bc -l)

# display resul
echo "$n"

# output
# 4.075

我使用这个sql查询从dbase中获取结果:

id entity_id   value
1    1        summer test
2    1        summer-test
3    2        winter test
4    2        winter-test

它以这种形式给我结果:

$query = 'SELECT entity_id,value FROM blogs';
$results = $readConnection->fetchAll($query);

现在我希望新数组如下:

   [0] => Array
                (
                    [entity_id] => 1   // this is common, so this should be key for new array
                    [value] => summer event
                )

            [1] => Array
                (
                    [entity_id] => 1
                    [value] => summer-event
                )

............... and so on....

我试过了:

 [1] => Array
        (
            [name] => summer event
            [path] => summer-event
        )

    [2] => Array
        (
            [name] => test event
            [path] => test-event
        )

它给出了所有entity_id的最后一个值

$newArray = [];
foreach($results as $v)
{
   $newArray[$v['entity_id']] = $v['value'];
}
print_r($newArray);

3 个答案:

答案 0 :(得分:1)

由于您的值取决于您现有的列,您可以尝试此查询来获取输出,然后您可以处理您的数组。

$query = 'SELECT value AS name,REPLACE(value ,' ','-') AS path FROM blogs';
$results = $readConnection->fetchAll($query);

在第二栏中,它将用你的( - )替换空格。
它会回报你

[0] => Array
        (
            [name] => summer event
            [path] => summer-event
        )

    [1] => Array
        (
            [name] => test event
            [path] => test-event
        )

答案 1 :(得分:1)

由于您没有将名称存储在数据库中,因此此处唯一的选择是在使用之前处理生成的数组。

// Create a new empty array
$proccessed_array = array();

// loop through the existing array and populate the new array
foreach ($results as $result) {
    $proccessed_array[] = array(
        'name' => str_replace('-', ' ', $result['value']), // You modify the path in order to be used as name
        'path' => $result['value'] // You keep this value to the new array
        );
}

答案 2 :(得分:0)

来自基于您的$results示例的数据集:

$results = [
    [
        'entity_id' => 3,
        'value' => 'Fall Event',
    ],
    [
        'entity_id' => 2,
        'value' => 'Summer Event',
    ],
    [
        'entity_id' => 1,
        'value' => 'Spring Event',
    ],
    [
        'entity_id' => 4,
        'value' => 'Winter Event',
    ],

];

$events = array_reduce($results, function ($carry, $item) {
    $carry[$item['entity_id']] = [
        'name' => $item['value'],
        'path' => $item['value'],
    ];

    return $carry;
}, $inital = []);

print_r($events);

显示以下内容:

Array
(
    [3] => Array
        (
            [name] => Fall Event
            [path] => Fall Event
        )

    [2] => Array
        (
            [name] => Summer Event
            [path] => Summer Event
        )

    [1] => Array
        (
            [name] => Spring Event
            [path] => Spring Event
        )

    [4] => Array
        (
            [name] => Winter Event
            [path] => Winter Event
        )

)